import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreAttachmentService extends BaseService<any> {
  private readonly logger = new Logger(CoreAttachmentService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * add
   * 对应 PHP: CoreAttachmentService_core::add()
   * 逻辑类型: undefined - undefined
   */
  async add(site_id: any[], data: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: $data['site_id'] = $site_id;         $attachment = $this->model->create($data);         if(!$attachment->att_id)             throw new AdminException(...
data.site_id = site_id;
        const attachment = this.model.create(data);
        if(!attachment.att_id)
            throw new AdminBusinessException('ADD_FAIL');//创建失败
        return attachment.att_id;
    }

    /**
     * 素材库模型对象
     * @param int site_id
     * @param int att_id
     * @return mixed
     */
    async find(int site_id, int att_id){
        const where = [
            ['site_id', '=', site_id],
            ['att_id', '=', att_id)
        ];
        const user = this.model.where(where).findOrEmpty(];
        if (user.isEmpty())
            throw new AdminBusinessException('ATTACHMENT_NOE_EXIST');
        return user;
    }

    /**
     * 图片列表
     * @param int site_id
     * @param array|null data
     * @return * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getList(int site_id, ?data = [)){
        const where = [
            ['site_id', '=', site_id)
        ];
        const att_ids = data.att_ids || [];
        if(!!att_ids)){
            where[] = ['att_id', 'in', att_ids];
  }

  /**
   * findRecord (原方法名: find)
   * 对应 PHP: CoreAttachmentService_core::find()
   * 逻辑类型: undefined - undefined
   * 注意: 为避免与BaseService方法冲突，已重命名
   */
  async findRecord(site_id: number, att_id: number) {
    // 基于PHP真实逻辑: find
    // PHP原文: $where = array(             ['site_id', '=', $site_id],             ['att_id', '=', $att_id]         );         $user = $this->model->where($where)->f...
const where = [
            ['site_id', '=', site_id],
            ['att_id', '=', att_id)
        ];
        const user = this.model.where(where).findOrEmpty(];
        if (user.isEmpty())
            throw new AdminBusinessException('ATTACHMENT_NOE_EXIST');
        return user;
    }

    /**
     * 图片列表
     * @param int site_id
     * @param array|null data
     * @return * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getList(int site_id, ?data = [)){
        const where = [
            ['site_id', '=', site_id)
        ];
        const att_ids = data.att_ids || [];
        if(!!att_ids)){
            where[] = ['att_id', 'in', att_ids];
  }

  /**
   * getList
   * 对应 PHP: CoreAttachmentService_core::getList()
   * 逻辑类型: undefined - undefined
   */
  async getList(site_id: any[], data: any[]) {
    // 基于PHP真实逻辑: getList
    // PHP原文: $where = array(             ['site_id', '=', $site_id]         );         $att_ids = $data['att_ids'] ?? [];         if(!empty($att_ids)){            ...
const where = [
            ['site_id', '=', site_id)
        ];
        const att_ids = data.att_ids || [];
        if(!!att_ids)){
            where[] = ['att_id', 'in', att_ids];
  }

  /**
   * edit
   * 对应 PHP: CoreAttachmentService_core::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(site_id: any[], att_id: any[], data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: $where = array(             ['site_id', '=', $site_id],             ['att_id', '=', $att_id]         );         return $this->model->update($data, $wh...
const where = [
            ['site_id', '=', site_id],
            ['att_id', '=', att_id)
        ];
        return this.model.update(data, where);
    }

    /**
     * 删除素材
     * @param int site_id
     * @param int att_id
     * @return mixed
     */
    async del(int site_id, int att_id){
        //查询是否有下级菜单或按钮
        const menu = this.find(site_id, att_id];
        return menu.delete();

    }


    /**
     * 删除附件库中的文件
     * @param int site_id
     * @param att_ids
     * @return true
     * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async delAll(int site_id, att_ids){
        const core_attachment_service = this.coreAttachmentService;
        const list = core_attachment_service.getList(site_id, compact('att_ids')];
        if(!list))
            throw new UploadFileBusinessException('PLEACE_SELECT_IMAGE'];

        const ids = array_column(list, 'att_id');
        foreach(list as v){
            const file_driver = this.coreFileService.driver(site_id, v.storage_type];
            //读取上传附件的信息用于后续得校验和数据写入,删除失败直接通过
            file_driver.delete(v.path);
  }

  /**
   * del
   * 对应 PHP: CoreAttachmentService_core::del()
   * 逻辑类型: undefined - undefined
   */
  async del(site_id: number, att_id: number) {
    // 基于PHP真实逻辑: del
    // PHP原文: //查询是否有下级菜单或按钮         $menu = $this->find($site_id, $att_id);         return $menu->delete();      }       /**      * 删除附件库中的文件      * @param int $si...
//查询是否有下级菜单或按钮
        const menu = this.find(site_id, att_id];
        return menu.delete();

    }


    /**
     * 删除附件库中的文件
     * @param int site_id
     * @param att_ids
     * @return true
     * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async delAll(int site_id, att_ids){
        const core_attachment_service = this.coreAttachmentService;
        const list = core_attachment_service.getList(site_id, compact('att_ids')];
        if(!list))
            throw new UploadFileBusinessException('PLEACE_SELECT_IMAGE'];

        const ids = array_column(list, 'att_id');
        foreach(list as v){
            const file_driver = this.coreFileService.driver(site_id, v.storage_type];
            //读取上传附件的信息用于后续得校验和数据写入,删除失败直接通过
            file_driver.delete(v.path);
  }

  /**
   * delAll
   * 对应 PHP: CoreAttachmentService_core::delAll()
   * 逻辑类型: undefined - undefined
   */
  async delAll(site_id: any[], att_ids: any[]) {
    // 基于PHP真实逻辑: delAll
    // PHP原文: $core_attachment_service = new CoreAttachmentService();         $list = $core_attachment_service->getList($site_id, compact('att_ids'));         if(em...
const core_attachment_service = this.coreAttachmentService;
        const list = core_attachment_service.getList(site_id, compact('att_ids')];
        if(!list))
            throw new UploadFileBusinessException('PLEACE_SELECT_IMAGE'];

        const ids = array_column(list, 'att_id');
        foreach(list as v){
            const file_driver = this.coreFileService.driver(site_id, v.storage_type];
            //读取上传附件的信息用于后续得校验和数据写入,删除失败直接通过
            file_driver.delete(v.path);
  }
}
